package com.naiterui.ehp.bs.b2c.pharmacy.service.impl;

import static com.naiterui.ehp.bs.b2c.pharmacy.entity.WarehouseGoods.STATUS_NORMAL;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.naiterui.ehp.bs.b2c.inventory.exception.StockServiceException;
import com.naiterui.ehp.bs.b2c.inventory.service.IStockService;
import com.naiterui.ehp.bs.b2c.pharmacy.bo.WarehouseGoodsInventoryBO;
import com.naiterui.ehp.bs.b2c.pharmacy.entity.Warehouse;
import com.naiterui.ehp.bs.b2c.pharmacy.entity.WarehouseGoods;
import com.naiterui.ehp.bs.b2c.pharmacy.repository.IWarehouseGoodsRepository;
import com.naiterui.ehp.bs.b2c.pharmacy.repository.IWarehouseRepository;
import com.naiterui.ehp.bs.b2c.pharmacy.service.IWarehouseGoodsService;
import com.naiterui.ehp.bs.b2c.product.service.ISkuService;

@Service
public class WarehouseGoodsServiceImpl implements IWarehouseGoodsService {

    @Autowired
    private ISkuService skuService;

    @Autowired
    private IStockService stockService;

    @Autowired
    private IWarehouseRepository warehouseRepository;

    @Autowired
    private IWarehouseGoodsRepository warehouseGoodsRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delSelectList(Long warehouseId, Set<Long> skuIds, String operator) throws StockServiceException {
        this.warehouseGoodsRepository.removeByWarehouseIdAndSkuIdIn(warehouseId, skuIds);
        this.setCooperateGoodsNumber(warehouseId, operator);
        //删除库存
        stockService.remove(warehouseId, skuIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setSelectList(Long warehouseId, Set<Long> skuIds, String operator) {

        List<WarehouseGoods> collect = this.skuService.getListBySkuIds(skuIds).stream()
                                                      .map(sku -> {
                                                          WarehouseGoods warehouseGoods = new WarehouseGoods();
                                                          warehouseGoods.setWarehouseId(warehouseId);
                                                          warehouseGoods.setProductId(sku.getProductId());
                                                          warehouseGoods.setSkuId(sku.getSkuId());
                                                          warehouseGoods.setName(sku.getFullName());
                                                          warehouseGoods.setNumber(sku.getNumber());
                                                          warehouseGoods.setPharmacologyClassificationId(sku.getPharmacologyClassificationId());
                                                          warehouseGoods.setStatus(STATUS_NORMAL);
                                                          warehouseGoods.setCreatedAt(new Date());
                                                          warehouseGoods.setCreatedBy(operator);
                                                          warehouseGoods.setChangedAt(new Date());
                                                          warehouseGoods.setChangedBy(operator);
                                                          return warehouseGoods;
                                                      }).collect(Collectors.toList());

        this.warehouseGoodsRepository.saveAll(collect);
        this.setCooperateGoodsNumber(warehouseId, operator);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInventory(Long warehouseId, Set<WarehouseGoodsInventoryBO> warehouseGoodsInventorys,
                                String operator) throws StockServiceException {
        List<WarehouseGoods> warehouseGoodsList = this.warehouseGoodsRepository.getByWarehouseIdAndStatus(warehouseId, STATUS_NORMAL);
        Set<Long> dbSkuIds = warehouseGoodsList.stream().map(WarehouseGoods::getSkuId).collect(Collectors.toSet());
        Set<Long> collect = warehouseGoodsInventorys.stream().map(WarehouseGoodsInventoryBO::getSkuId).collect(Collectors.toSet());
        if (!dbSkuIds.containsAll(collect)) {
            throw new IllegalArgumentException(String.format("当前商家没有合作该商品 warehouseId:%d sku:%s", warehouseId, collect.removeAll(dbSkuIds)));
        }
        //开始修改库存
        this.stockService.updateInventory(warehouseId, warehouseGoodsInventorys, operator);
    }

    /**
     * 修改合作商品数量
     */
    private void setCooperateGoodsNumber(Long warehouseId, String operator) {
        Warehouse warehouse = this.warehouseRepository.get(warehouseId);
        warehouse.setNumber(this.warehouseGoodsRepository.countByWarehouseIdAndStatus(warehouseId, STATUS_NORMAL));
        warehouse.setChangedAt(new Date());
        warehouse.setChangedBy(operator);
        this.warehouseRepository.update(warehouse);
    }

}
